import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import BackendService

ApplicationWindow {
    id: serverHostWindow
    width: 450
    height: 300
    visible: false
    title: "服务器地址设置"
    flags: Qt.FramelessWindowHint | Qt.Window
    color: "transparent"
    
    property string currentServerHost: ""
    
    // 窗口容器
    Rectangle {
        id: container
        anchors.fill: parent
        color: "#ffffff"
        radius: 8
        border.color: "#d0d0d0"
        border.width: 1
        clip: true
        
        // 窗口拖拽区域
        MouseArea {
            anchors.fill: parent
            property point startMousePos
            property point startWindowPos
            
            onPressed: {
                startMousePos = Qt.point(mouseX, mouseY);
                startWindowPos = Qt.point(serverHostWindow.x, serverHostWindow.y);
            }
            
            onPositionChanged: {
                if (pressed) {
                    var deltaX = mouseX - startMousePos.x;
                    var deltaY = mouseY - startMousePos.y;
                    serverHostWindow.x = startWindowPos.x + deltaX;
                    serverHostWindow.y = startWindowPos.y + deltaY;
                }
            }
        }
        
        // 窗口工具栏
        WindowToolBar {
            id: toolbar
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.margins: 1
            mainWindow: serverHostWindow
        }
        
        ColumnLayout {
            anchors.top: toolbar.bottom
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            anchors.margins: 30
            spacing: 20
            
            // 标题
            Text {
                text: "服务器地址设置"
                font.pixelSize: 20
                font.bold: true
                color: "#333333"
                Layout.alignment: Qt.AlignHCenter
            }
            
            // 服务器地址输入框
            ColumnLayout {
                Layout.fillWidth: true
                spacing: 5
                
                Text {
                    text: "服务器地址"
                    font.pixelSize: 14
                    color: "#666666"
                }
                
                TextField {
                    id: serverHostField
                    Layout.fillWidth: true
                    placeholderText: "请输入服务器地址（如：http://localhost:8888）"
                    font.pixelSize: 14
                    leftPadding: 8
                    rightPadding: 8
                    topPadding: 8
                    bottomPadding: 8
                    background: Rectangle {
                        color: "transparent"
                        border.color: "#d0d0d0"
                        border.width: 1
                        radius: 5
                    }
                    MouseArea {
                        anchors.fill: parent
                        propagateComposedEvents: true
                        onPressed: mouse.accepted = false
                    }
                }
            }
            
            // 状态提示
            Text {
                id: statusText
                Layout.fillWidth: true
                font.pixelSize: 12
                color: "#666666"
                visible: text !== ""
                wrapMode: Text.WordWrap
            }
            
            // 按钮区域
            RowLayout {
                Layout.fillWidth: true
                spacing: 10
                
                Button {
                    text: "测试连接"
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: 36
                    enabled: serverHostField.text.trim() !== ""
                    onClicked: {
                        testConnection();
                    }
                    background: Rectangle {
                        color: parent.enabled ? (parent.pressed ? "#1976d2" : "#2196f3") : "#cccccc"
                        radius: 4
                    }
                    contentItem: Text {
                        text: parent.text
                        font: parent.font
                        color: "#ffffff"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                }
                
                Item {
                    Layout.fillWidth: true
                }
                
                Button {
                    text: "取消"
                    Layout.preferredWidth: 80
                    Layout.preferredHeight: 36
                    onClicked: {
                        serverHostWindow.close();
                    }
                    background: Rectangle {
                        color: parent.pressed ? "#e0e0e0" : "#f5f5f5"
                        border.color: "#d0d0d0"
                        border.width: 1
                        radius: 4
                    }
                    contentItem: Text {
                        text: parent.text
                        font: parent.font
                        color: "#333333"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                }
                
                Button {
                    text: "保存"
                    Layout.preferredWidth: 80
                    Layout.preferredHeight: 36
                    enabled: serverHostField.text.trim() !== ""
                    onClicked: {
                        saveServerHost();
                    }
                    background: Rectangle {
                        color: parent.enabled ? (parent.pressed ? "#1976d2" : "#2196f3") : "#cccccc"
                        radius: 4
                    }
                    contentItem: Text {
                        text: parent.text
                        font: parent.font
                        color: "#ffffff"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                }
            }
            
            Item {
                Layout.fillHeight: true
            }
        }
    }
    
    // 测试连接
    function testConnection() {
        var serverHost = serverHostField.text.trim();
        if (serverHost === "") {
            statusText.text = "请输入服务器地址";
            statusText.color = "#e81123";
            return;
        }
        
        statusText.text = "正在测试连接...";
        statusText.color = "#666666";
        
        // 调用后端服务测试连接
        var result = backendService.testServerHost(serverHost);
        if (result) {
            statusText.text = "连接测试成功！";
            statusText.color = "#4caf50";
        } else {
            statusText.text = "连接测试失败，请检查服务器地址格式";
            statusText.color = "#e81123";
        }
    }
    
    // 保存服务器地址
    function saveServerHost() {
        var serverHost = serverHostField.text.trim();
        if (serverHost === "") {
            statusText.text = "请输入服务器地址";
            statusText.color = "#e81123";
            return;
        }
        
        // 先测试连接
        if (!backendService.testServerHost(serverHost)) {
            statusText.text = "服务器地址格式无效，请检查后重试";
            statusText.color = "#e81123";
            return;
        }
        
        // 保存服务器地址
        backendService.saveServerHost(serverHost);
        statusText.text = "服务器地址已保存";
        statusText.color = "#4caf50";
        
        // 延迟关闭窗口
        saveTimer.start();
    }
    
    // 延迟关闭定时器
    Timer {
        id: saveTimer
        interval: 1000
        onTriggered: {
            serverHostWindow.close();
        }
    }
    
    // 窗口显示时加载当前服务器地址
    onVisibleChanged: {
        if (visible) {
            var serverHost = backendService.loadServerHost();
            if (serverHost !== "") {
                serverHostField.text = serverHost;
                currentServerHost = serverHost;
            } else {
                serverHostField.text = "";
                currentServerHost = "";
            }
            statusText.text = "";
        }
    }
}

